package com.qdsg.ysg.doctor.ui.kotlin

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.qdsg.ysg.doctor.R
import com.qdsg.ysg.doctor.base.BaseActivity
import com.qdsg.ysg.doctor.ui.ChatActivity
import com.qdsg.ysg.doctor.ui.SendMessageActivity
import com.qdsg.ysg.doctor.ui.WebViewActivity
import com.qdsg.ysg.doctor.ui.activity.prescription.AddCaseActivity
import com.qdsg.ysg.doctor.ui.activity.prescription.PrescriptionListActivity
import com.qdsg.ysg.doctor.util.ImageLoaderHelper
import com.qdsg.ysg.doctor.util.ToastUtil
import com.qdsg.ysg.doctor.util.UtilString
import com.qdsg.ysg.doctor.viewmodel.CloudListViewModel
import kotlinx.android.synthetic.main.aty_base_list.*
import kotlinx.android.synthetic.main.include_empty.*
import kotlinx.android.synthetic.main.include_title.*

class CloudListActivity : BaseActivity() {

    private var flagStatus: String? = null
    lateinit var myAdapter: MyAdapter
    private val viewModel by lazy { ViewModelProviders.of(this).get(CloudListViewModel::class.java) }


    override fun initViews() {
        val layoutManager = LinearLayoutManager(this)
        myAdapter = MyAdapter()
        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = myAdapter

        refreshLayout.setPrimaryColors(-0x1, -0xaa4757)
        refreshLayout.isEnableLoadMore = false
        refreshLayout.setOnRefreshListener {
            viewModel.getCloudList(flagStatus!!)
        }
        refreshLayout.setOnLoadMoreListener { }

        viewModel.dataChanged.observe(this, Observer {
            refreshLayout.visibility = View.VISIBLE
            cl_empty.visibility = View.GONE
            myAdapter.notifyDataSetChanged()
            refreshLayout.finishRefresh(true)
        })

        viewModel.dataEmpty.observe(this, Observer {
            refreshLayout.visibility = View.GONE
            cl_empty.visibility = View.VISIBLE
        })

        when (flagStatus) {
            "302" -> tv_title.text = "云门诊进行中"
            "300" -> tv_title.text = "全部预约"
            "304" -> tv_title.text = "历史记录"
        }

    }

    override fun initData() {
        flagStatus = intent.getStringExtra("flag")

    }

    override fun onResume() {
        super.onResume()
        viewModel.getCloudList(flagStatus!!)
    }

    override fun getLayoutResource(): Int {
        return R.layout.aty_base_list
    }

    inner class MyAdapter : RecyclerView.Adapter<MyAdapter.Holder>() {


        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
            return Holder(LayoutInflater.from(mContext).inflate(R.layout.item_cloud, parent, false))
        }

        override fun getItemCount(): Int {
            return viewModel.list.size
        }

        @SuppressLint("SetTextI18n")
        override fun onBindViewHolder(holder: Holder, position: Int) {
            holder.tv_name.text = viewModel.list[position].pName
            holder.tv_detail.text = "${viewModel.list[position].pSex}  ${viewModel.list[position].pAge}  "
            holder.tv_time.text = "预约时间:${viewModel.list[position].scheduleTime}"
            holder.btn_confirm.visibility = View.GONE
            holder.tv_end_chat.visibility = View.GONE
            val oFlag = viewModel.list[position].oFlag
            val isSocial = viewModel.list[position].isSocial
            val avatar = viewModel.list[position].avatar
            val pSex = viewModel.list[position].pSex
            holder.itemView.setOnClickListener {
                val bundle = Bundle()
                bundle.putSerializable("diagnoseId", viewModel.list[position].oId)
                bundle.putSerializable("oFlag", viewModel.list[position].oFlag)
                bundle.putSerializable("readyFlag", viewModel.list[position].readyFlag)
                startActivity(ChatActivity::class.java, bundle)

//                if (viewModel.list[position].oFlag == 302 && viewModel.list[position].readyFlag == 1) {
//                    viewModel.changeFlag(viewModel.list[position].oId, "303")
//                }
            }
            if ("304".equals(flagStatus)) {
                holder.tv_look_chufang.visibility = View.GONE
                holder.tv_send_sms.visibility = View.GONE
                holder.tv_three.visibility = View.GONE
            } else {
                holder.tv_look_chufang.visibility = View.VISIBLE
                holder.tv_send_sms.visibility = View.VISIBLE
                holder.tv_three.visibility = View.VISIBLE
            }
            when (viewModel.list[position].oFlag) {
                301 -> holder.tv_status.text = "代付款"
                302 -> if (viewModel.list[position].readyFlag == 1) {
                    holder.tv_status.text = "已报到"
                } else {
                    holder.tv_status.text = "未报到"
                }
                303 -> holder.tv_status.text = "进行中"
                304 -> holder.tv_status.text = "已结束"
                305 -> holder.tv_status.text = "已爽约"
                306 -> holder.tv_status.text = "已取消"
                307 -> holder.tv_status.text = "医保支付失败"
                308 -> holder.tv_status.text = "等待结束"
            }

            if (oFlag == 304) {
                holder.tv_status.setBackgroundResource(R.drawable.shape_solid_main)
                if (viewModel.list[position].supplementFlag == 0) {
                    //24小时之后，补充处方按钮消失
                    holder.btn_confirm.visibility = View.GONE
                } else {
                    holder.btn_confirm.visibility = View.VISIBLE
                }
                holder.btn_confirm.text = "补充处方"
                holder.btn_confirm.setOnClickListener {
                    val bundle = Bundle()
                    bundle.putSerializable("diagnoseId", viewModel.list[position].oId)
                    startActivity(PrescriptionListActivity::class.java, bundle)
                }
            } else if (oFlag == 305 || oFlag == 306 || oFlag == 307) {
                holder.tv_status.setTextColor(Color.parseColor("#999999"))
                holder.tv_status.setBackgroundResource(R.drawable.shape_solid_f6f6f6)
                holder.tv_name.setTextColor(Color.parseColor("#999999"))
                holder.tv_name.setTextColor(ContextCompat.getColor(mContext, R.color.commonGrey))
                holder.tv_detail.setTextColor(ContextCompat.getColor(mContext, R.color.commonGrey))
                holder.tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.commonGrey))

            } else if (oFlag == 308) {
                //等待结束状态的诊断，需手动点击结束诊断或者在当天24点自动结束
                holder.tv_end_chat.visibility = View.VISIBLE
                holder.tv_look_chufang.setText("查看处方")
                holder.tv_status.setBackgroundResource(R.drawable.shape_solid_main)
                holder.tv_status.setBackgroundResource(R.drawable.shape_solid_main)
                holder.tv_look_chufang.setBackgroundResource(R.drawable.shape_stroke_main)
                holder.tv_send_sms.setBackgroundResource(R.drawable.shape_stroke_main)
                holder.tv_three.setBackgroundResource(R.drawable.shape_stroke_main)
                holder.tv_look_chufang.setTextColor(ContextCompat.getColor(mContext, R.color.mainGreen))
                holder.tv_send_sms.setTextColor(ContextCompat.getColor(mContext, R.color.mainGreen))
                holder.tv_three.setTextColor(ContextCompat.getColor(mContext, R.color.mainGreen))
            } else if (oFlag == 303) {
                holder.ll_cloud.setBackgroundColor(ContextCompat.getColor(mContext, R.color.E1455b8a9))
                holder.tv_look_chufang.setText("诊断意见")
                holder.tv_status.setBackgroundResource(R.drawable.shape_solid_main)
                holder.tv_look_chufang.setBackgroundResource(R.drawable.shape_stroke_main)
                holder.tv_send_sms.setBackgroundResource(R.drawable.shape_stroke_main)
                holder.tv_three.setBackgroundResource(R.drawable.shape_stroke_main)
                holder.tv_look_chufang.setTextColor(ContextCompat.getColor(mContext, R.color.mainGreen))
                holder.tv_send_sms.setTextColor(ContextCompat.getColor(mContext, R.color.mainGreen))
                holder.tv_three.setTextColor(ContextCompat.getColor(mContext, R.color.mainGreen))

            } else if (oFlag == 302) {

                if (viewModel.list[position].readyFlag == 1) {
                    //已报道
                    holder.tv_look_chufang.setText("开始诊断")
//                    showmainGreen()
                    holder.tv_look_chufang.setBackgroundResource(R.drawable.shape_stroke_main)
                    holder.tv_send_sms.setBackgroundResource(R.drawable.shape_stroke_main)
                    holder.tv_three.setBackgroundResource(R.drawable.shape_stroke_main)
                    holder.tv_look_chufang.setTextColor(ContextCompat.getColor(mContext, R.color.mainGreen))
                    holder.tv_send_sms.setTextColor(ContextCompat.getColor(mContext, R.color.mainGreen))
                    holder.tv_three.setTextColor(ContextCompat.getColor(mContext, R.color.mainGreen))
                } else {
                    //未报到
                    holder.tv_look_chufang.setText("开始诊断")
                    holder.tv_status.setTextColor(Color.parseColor("#999999"))
                    holder.tv_status.setBackgroundResource(R.drawable.shape_solid_f6f6f6)
                    holder.tv_look_chufang.setBackgroundResource(R.drawable.shape_stroke_gray)
                    holder.tv_look_chufang.setTextColor(Color.parseColor("#999999"))
                    holder.tv_send_sms.setBackgroundResource(R.drawable.shape_stroke_gray)
                    holder.tv_send_sms.setTextColor(Color.parseColor("#999999"))
                    holder.tv_three.setBackgroundResource(R.drawable.shape_stroke_gray)
                    holder.tv_three.setTextColor(Color.parseColor("#999999"))
                    holder.tv_name.setTextColor(ContextCompat.getColor(mContext, R.color.commonGrey))
                    holder.tv_detail.setTextColor(ContextCompat.getColor(mContext, R.color.commonGrey))
                    holder.tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.commonGrey))

                }
                holder.btn_confirm.setOnClickListener {
                    val bundle = Bundle()
                    bundle.putSerializable("diagnoseId", viewModel.list[position].oId)
                    bundle.putSerializable("oFlag", viewModel.list[position].oFlag)
                    bundle.putSerializable("readyFlag", viewModel.list[position].readyFlag)
                    startActivity(ChatActivity::class.java, bundle)

                    if (viewModel.list[position].oFlag == 302 && viewModel.list[position].readyFlag == 1) {
                        viewModel.changeFlag(viewModel.list[position].oId, "303")
                    }
                }
            }

            if ("0".equals(isSocial)) {
                holder.iv_yibao.setImageResource(R.mipmap.icon_no_yibao)
            } else {
                holder.iv_yibao.setImageResource(R.mipmap.icon_yibao)
            }
            if (!UtilString.isEmpty(avatar)) {
                ImageLoaderHelper.getInstance().GlideCircleImageLoader(mContext, avatar, holder.iv_head)
            } else if ("女".equals(pSex)) {
                holder.iv_head.setImageDrawable(ContextCompat.getDrawable(mContext, R.mipmap.icon_girl))
            } else {
                holder.iv_head.setImageDrawable(ContextCompat.getDrawable(mContext, R.mipmap.icon_boy))
            }

            holder.tv_look_chufang.setOnClickListener {
                //查看处方
                if (oFlag == 308) {
                    val bundle = Bundle()
                    bundle.putSerializable("diagnoseId", viewModel.list[position].oId)
                    startActivity(PrescriptionListActivity::class.java, bundle)
                } else if (oFlag == 303) {
                    //进行中，去写病历
                    val bundle = Bundle()
                    bundle.putSerializable("patientId", viewModel.list[position].patientId)
                    bundle.putSerializable("diagnoseId", viewModel.list[position].oId)
                    startActivity(AddCaseActivity::class.java, bundle)
                } else if (viewModel.list[position].oFlag == 302 && viewModel.list[position].readyFlag == 1) {
                    //已报到，点击开始诊断
                    viewModel.changeFlag(viewModel.list[position].oId, "303")
                    viewModel.getCloudList(flagStatus!!)
                    val bundle = Bundle()
                    bundle.putSerializable("diagnoseId", viewModel.list[position].oId)
                    bundle.putSerializable("oFlag", 303)
                    bundle.putBoolean("isFromCloudList", true)
                    bundle.putSerializable("readyFlag", viewModel.list[position].readyFlag)
                    startActivity(ChatActivity::class.java, bundle)

//                    myAdapter.notifyDataSetChanged()

                } else {
                    ToastUtil.showToast(mContext, "当前状态不能开始诊断")
                }

            }
            holder.tv_send_sms.setOnClickListener {
                //短信通知
                val bundle = Bundle()
                bundle.putSerializable("outpId", viewModel.list[position].oId)
                startActivity(SendMessageActivity::class.java, bundle)


            }
            holder.tv_three.setOnClickListener {
                val bundle = Bundle()
                bundle.putString("patientId", viewModel.list[position].patientId)
                bundle.putString("fromWhere", "CloudListActivity")
                startActivity(WebViewActivity::class.java, bundle)

            }
            holder.tv_end_chat.setOnClickListener {
                //结束诊断
                viewModel.changeFlag(viewModel.list[position].oId, "304")
                viewModel.getCloudList(flagStatus!!)
            }


        }


        inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val tv_name = itemView.findViewById<TextView>(R.id.tv_name)
            val tv_detail = itemView.findViewById<TextView>(R.id.tv_detail)
            val tv_time = itemView.findViewById<TextView>(R.id.tv_time)
            val tv_status = itemView.findViewById<TextView>(R.id.tv_status)
            val tv_end_chat = itemView.findViewById<TextView>(R.id.tv_end_chat)
            val iv_yibao = itemView.findViewById<ImageView>(R.id.iv_yibao)
            val iv_head = itemView.findViewById<ImageView>(R.id.iv_head)
            val tv_look_chufang = itemView.findViewById<TextView>(R.id.tv_look_chufang)
            val tv_send_sms = itemView.findViewById<TextView>(R.id.tv_send_sms)
            val tv_three = itemView.findViewById<TextView>(R.id.tv_three)
            val btn_confirm = itemView.findViewById<TextView>(R.id.btn_confirm)
            val ll_cloud = itemView.findViewById<LinearLayout>(R.id.ll_cloud)
        }

    }
}